﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.dao;

namespace BOM.BaseData
{
    public partial class CustomerList : Form
    {
        //这个标记点击datagroupview中某一行的ID
        public static int XuanZID = -1;
        //这个变量是标记用户是点击了 查看 还是标记
        public static int BiaoJi = -1; //查看为 0 修改为 1 新增 为 2

        public CustomerList()
        {
            InitializeComponent();
        }
        //新增按钮
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            BiaoJi = 2; //标记是否为新增
            Customer customer = new Customer();
            customer.Owner = this;
            customer.ShowDialog();
            //if (customer.DialogResult == DialogResult.No)
            //{
            //    CustomerNew customerGenXin = new CustomerNew();
            //    dataCustomer.DataSource = customerGenXin.CustomerListChaKan();
            //}
        }
        //查看按钮
        private void btnView_Click(object sender, EventArgs e)
        {
            //string str = dataCustomer.CurrentRow.Cells[0].Value.ToString();//获取当前选中行的ID
            //MessageBox.Show(str);
            try
            {
                CustomerNew customer = new CustomerNew();
                XuanZID = Convert.ToInt32(dataCustomer.CurrentRow.Cells[0].Value);
                if (customer.CustomerChak(XuanZID) == 1)
                {
                    Customer ChaK = new Customer();
                    BiaoJi = 0;//标记是否点击查看
                    ChaK.Owner = this;
                    ChaK.ShowDialog();
                }
                else
                {
                    MessageBox.Show("该记录已被删除", "提示");
                }
            }
            catch
            { }
        }
        //把数据添加到datagridview中
        public void BinData()
        {
            CustomerNew customer = new CustomerNew();
            dataCustomer.DataSource = customer.CustomerListChaKan();
        }
        //窗体的加载事件
        private void CustomerList_Load(object sender, EventArgs e)
        {
            dataCustomer.AutoGenerateColumns = false;
            BinData();
        }
        //修改按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                XuanZID = Convert.ToInt32(dataCustomer.CurrentRow.Cells[0].Value);
                Customer ChaK = new Customer();
                BiaoJi = 1;
                ChaK.Owner = this;
                ChaK.ShowDialog();
            }
            catch
            { }
        }
        //删除按钮
        private void BtnRemove_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                try
                {
                    CustomerNew customer = new CustomerNew();
                    XuanZID = Convert.ToInt32(dataCustomer.CurrentRow.Cells[0].Value);
                    //MessageBox.Show(XuanZID.ToString());
                    customer.CustomerListShanChu(XuanZID);
                    BinData();
                    dataCustomer.AutoGenerateColumns = false;
                    MessageBox.Show("删除成功", "提示");
                }
                catch
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
        }
        //刷新按钮
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            CustomerNew customer = new CustomerNew();
            dataCustomer.DataSource = customer.CustomerListChaKan();
        }
        //退出按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void dataCustomer_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }
        private void dataCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        //用户双击时发生的事件
        private void dataCustomer_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                XuanZID = Convert.ToInt32(dataCustomer.CurrentRow.Cells[0].Value);
                Customer ChaK = new Customer();
                BiaoJi = 1;
                ChaK.Owner = this;
                ChaK.ShowDialog();
            }
            catch
            { }
        }
    }
}
